﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CB.Client
{
    public class CatHead : MonoBehaviour
    {
        enum OpStatus
        {
            None,
            Down,
            Dragging
        }

        OpStatus Status;

        private event Action<float, float> OnClicked;
        public event Action<float, float> OnStartDragging;
        public event Action<float, float, float, float> OnDragging;
        public event Action<float, float, float, float> OnEndDragging;

        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {

        }

        Vector2 dragFrom;
        private void OnMouseDown()
        {
            if (!CheckPointerRelativePosition(out Vector2 v2)) return;

            Status = OpStatus.Down;
            dragFrom = v2;
        }

        private void OnMouseUp()
        {
            if (Status == OpStatus.Down)
            {
                Debug.Log($"Clicked {name}");
                OnClicked?.Invoke(dragFrom.x, dragFrom.y);
            }

            else if (Status == OpStatus.Dragging)
            {
                Debug.Log("Drag end");
                OnEndDragging?.Invoke(dragFrom.x, dragFrom.y, lastDraggingPos.x, lastDraggingPos.y);
            }

            Status = OpStatus.None;
        }

        Vector2 draggingOffset;
        Vector2 lastDraggingPos;
        private void OnMouseDrag()
        {
            if (!CheckPointerRelativePosition(out Vector2 pt)) return;

            if (Status == OpStatus.Down)
            {
                if ((pt - dragFrom).sqrMagnitude <= 0.1f)
                    return;

                Status = OpStatus.Dragging;
                Debug.Log($"strat dragging {name}");
                OnStartDragging?.Invoke(dragFrom.x, dragFrom.y);
                lastDraggingPos = dragFrom;
                draggingOffset = Vector2.zero;
            }
            else if (Status == OpStatus.Dragging)
            {
                var dPos = pt - lastDraggingPos;
                lastDraggingPos = pt;
                draggingOffset += dPos;
                OnDragging?.Invoke(dragFrom.x, dragFrom.y, dragFrom.x + draggingOffset.x, dragFrom.y + draggingOffset.y);
            }
        }

        bool CheckPointerRelativePosition(out Vector2 v2, int pt = 0)
        {
            v2 = Vector2.zero;

            // 点在 ui 上就不处理了
            if (this.CheckGuiRaycastObjects())
                return false;

            var ray = Camera.main.ScreenPointToRay(Input.touchCount == 0 ? (Vector2)Input.mousePosition : Input.touches[0].position);
            var hitInfos = Physics2D.GetRayIntersectionAll(ray);
            if (hitInfos.Length > 0 && hitInfos[0].collider?.gameObject == gameObject)
            {
                v2 = hitInfos[0].point;
                return true;
            }

            return false;
        }
    }
}